//
//  HOStaticDataMgr.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/21/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOStaticDataMgr.h"
#import "HODepartmentEntity.h"
#import "HOFaultCategoryEntity.h"
#import "HOHopeHanderEntity.h"
#import "HOConstants.h"
#import "HOAreaEntity.h"
#import "HOUIMgr.h"

static HOStaticDataMgr *instance;
@interface HOStaticDataMgr()

@property(nonatomic, retain)NSMutableDictionary *departmentDic;
@property(nonatomic, retain)NSMutableDictionary *hopeHandlerDic;
@property(nonatomic, retain)NSMutableDictionary *faultCategoryDic;
@property(nonatomic, retain)NSMutableDictionary *faultLocationDic;
@property(nonatomic, retain)NSMutableDictionary *faultTypeDic;
@property(nonatomic, retain)NSMutableDictionary *faultAllLocationDic;
@property(nonatomic, retain)HOUserInfoEntity *currentUserInfo;
@property(nonatomic, retain)NSMutableDictionary *areaDic;
@end
@implementation HOStaticDataMgr

+ (HOStaticDataMgr *)sharedInstance
{
	if(!instance)
	{
		instance = [[HOStaticDataMgr alloc] init];
	}
	return instance;
}

- (instancetype)init
{
	self = [super init];
	if(self)
	{
		self.currentUserInfo = [[[HOUserInfoEntity alloc] init] autorelease];
		self.hopeHanderList = [[[NSMutableArray alloc] init] autorelease];
		self.departmentList = [[[NSMutableArray alloc] init] autorelease];
		self.faultCategoryList = [[[NSMutableArray alloc] init] autorelease];
		self.faultLocationList = [[[NSMutableArray alloc] init] autorelease];
		self.faultLocationList = [[[NSMutableArray alloc] init] autorelease];
		self.faultAllLocationDic = [[[NSMutableDictionary alloc] init] autorelease];
		self.faultLocationDic = [[[NSMutableDictionary alloc] init] autorelease];
		self.faultTypeDic = [[[NSMutableDictionary alloc] init] autorelease];
		
		self.departmentDic = [[[NSMutableDictionary alloc] init] autorelease];
		self.hopeHandlerDic = [[[NSMutableDictionary alloc] init] autorelease];
		self.faultCategoryDic = [[[NSMutableDictionary alloc] init] autorelease];
		
		self.buildingList = [[[NSMutableArray alloc] init] autorelease];
		self.areaDic = [[[NSMutableDictionary alloc] init] autorelease];
	}
	return self;
}

- (void)cleanup
{
	[self.currentUserInfo cleanup];
	[self.hopeHanderList removeAllObjects];
	[self.departmentList removeAllObjects];
	[self.faultCategoryList removeAllObjects];
	[self.faultAllLocationDic removeAllObjects];
	[self.faultLocationDic removeAllObjects];
	[self.faultLocationList removeAllObjects];
	[self.faultLocationList removeAllObjects];
	[self.faultTypeDic removeAllObjects];
	
	[self.departmentDic removeAllObjects];
	[self.hopeHandlerDic removeAllObjects];
	[self.faultCategoryDic removeAllObjects];
	[self.buildingList removeAllObjects];
	[self.areaDic removeAllObjects];
}

- (void)dealloc
{
	self.currentUserInfo = nil;
	self.faultLocationDic = nil;
	self.hopeHanderList = nil;
	self.departmentList = nil;
	self.faultCategoryDic = nil;
	self.faultCategoryList = nil;
	self.faultAllLocationDic = nil;
	self.departmentDic = nil;
	self.hopeHandlerDic = nil;
	self.faultCategoryDic = nil;
	self.buildingList = nil;
	self.areaDic = nil;
	self.faultLocationList = nil;
	self.faultLocationList = nil;
	
	[super dealloc];
}

- (HOUserInfoEntity *)getUserInfo
{
	return self.currentUserInfo;
}

- (BOOL)isDirectorUser
{	
	return self.currentUserInfo.userId == [self getDirectorId];
}

- (int)getDirectorId
{
//	if([self.currentUserInfo.companyId isEqualToString:kQingChun])
//	{
//		return Qingchun_DIRECTIORID;
//	}
//	else
//	{
//		return Xiasha_DIRECTIORID;
//	}
	return 0;
}


- (NSString *)getCompanyId
{
	if(self.currentUserInfo.companyId)
		return self.currentUserInfo.companyId;
	
	NSString *token =[[HOUIMgr sharedInstance] getToken];
	//c1cdf708c4b297e63c2c8d6b743e0d92_00101
	if(token != nil)
	{
		NSArray *params = [token componentsSeparatedByString:@"_"];
		if([params count] == 2)
		{
			self.currentUserInfo.companyId = [params lastObject];
			return self.currentUserInfo.companyId;
		}
	}
	return @"";
}

- (HODepartmentEntity *)getDepartmentEntityWithId:(int)departmentId
{
	HODepartmentEntity *entity = [self.departmentDic objectForKey:[NSNumber numberWithInt:departmentId]];
	if(!entity)
	{
		entity = [[HODepartmentEntity alloc] init];
		entity.departmentId = departmentId;
		[self.departmentDic setObject:entity forKey:[NSNumber numberWithInt:departmentId]];
		[entity release];
	}
	entity = [self.departmentDic objectForKey:[NSNumber numberWithInt:departmentId]];
	return entity;
}

- (HOFaultCategoryEntity *)getFaultCategoryEntityWithId:(int)faultId
{
	HOFaultCategoryEntity *entity = [self.faultCategoryDic objectForKey:[NSNumber numberWithInt:faultId]];
	if(!entity)
	{
		entity = [[HOFaultCategoryEntity alloc] init];
		entity.faultId = faultId;
		[self.faultCategoryDic setObject:entity forKey:[NSNumber numberWithInt:faultId]];
		[entity release];
	}
	return entity;
}

- (HOHopeHanderEntity *)getHopeHandlerEntityWithId:(int)handlerId
{
	HOHopeHanderEntity *entity = [self.hopeHandlerDic objectForKey:[NSNumber numberWithInt:handlerId]];
	if(!entity)
	{
		entity = [[HOHopeHanderEntity alloc] init];
		entity.userId = handlerId;
		[self.hopeHandlerDic setObject:entity forKey:[NSNumber numberWithInt:handlerId]];
		[entity release];
	}
	entity = [self.hopeHandlerDic objectForKey:[NSNumber numberWithInt:handlerId]];
	return entity;
}

- (HOAreaEntity *)getAreaEntityWithId:(int)areaId
{
	if(areaId == 0)
		return nil;
	HOAreaEntity *entity = [self.areaDic objectForKey:[NSNumber numberWithInt:areaId]];
	if(!entity)
	{
		entity = [[[HOAreaEntity alloc] init] autorelease];
		entity.areaId = areaId;
		[self.areaDic setObject:entity forKey:[NSNumber numberWithInt:areaId]];
	}
	return entity;
}

- (void)didReceiveFaultCategoryList:(NSArray *)array
{
	for(NSInteger i = 0; i < [array count];i ++)
	{
		NSDictionary *dic = [array objectAtIndex:i];
		int entityId = [[dic objectForKey:kHO_FIELD_FaultId] intValue];
		HOFaultCategoryEntity *entity = [self getFaultCategoryEntityWithId:entityId];
		[entity parseDic:dic];
		if(![self.faultCategoryList containsObject:entity])
		{
			[self.faultCategoryList addObject:entity];
		}
	}
}

- (void)didReceiveDepartmentList:(NSArray *)array
{
	for(NSInteger i = 0; i < [array count];i ++)
	{
		NSDictionary *dic = [array objectAtIndex:i];
		int entityId = [[dic objectForKey:kHO_FIELD_DepartmentId] intValue];
		HODepartmentEntity *entity = [self getDepartmentEntityWithId:entityId];
		[entity parseDic:dic];
		if(![self.departmentList containsObject:entity])
		{
			[self.departmentList addObject:entity];
		}
	}
}


- (void)didReceiveHopeHandlerList:(NSArray *)array
{
	for(NSInteger i = 0; i < [array count];i ++)
	{
		NSDictionary *dic = [array objectAtIndex:i];
		int entityId = [[dic objectForKey:kHO_FIELD_HopeId] intValue];
		HOHopeHanderEntity *entity = [self getHopeHandlerEntityWithId:entityId];
		[entity parseDic:dic];
		if(![self.hopeHanderList containsObject:entity])
		{
			[self.hopeHanderList addObject:entity];
		}
	}
}

- (void)didReceiveFaultLocationList:(NSArray *)array
{

}

- (void)didReceiveBuildingList:(NSArray *)array
{
	for(NSInteger i = 0; i < [array count];i ++)
	{
		NSDictionary *dic = [array objectAtIndex:i];
		NSString *buildingName = [dic objectForKey:kHO_FIELD_AreaName];
		[self.buildingList addObject:buildingName];
		NSArray *areaArray = [dic objectForKey:kHO_FIELD_Buildings];
		
		NSMutableArray *dataArray = [[NSMutableArray alloc] init];
		for(NSInteger j = 0; j < [areaArray count]; j ++)
		{
			
			NSDictionary *areaDic = [areaArray objectAtIndex:j];
			int entityId = [[areaDic objectForKey:kHO_FIELD_AreaId] intValue];
			HOAreaEntity *entity = [self getAreaEntityWithId:entityId];
			[entity parseDic:areaDic];
			[dataArray addObject:entity];
		}
		[self.areaDic setObject:dataArray forKey:buildingName];
		[dataArray release];
	}
}

- (NSArray *)getAreaListWithBuildingName:(NSString *)buildingName
{
	if(buildingName == nil)
		return [NSArray array];
	return [self.areaDic objectForKey:buildingName];
	
}

- (NSMutableArray *)getFirstTypeArrayWithDepartmentId:(int)deptId
{
	NSMutableArray *array = [self.faultTypeDic objectForKey:[NSNumber numberWithInt:deptId]];
	if(array == nil)
	{
		array = [[[NSMutableArray alloc] init] autorelease];
		[self.faultTypeDic setObject:array forKey:[NSNumber numberWithInt:deptId]];
		
	}
	return array;
}

- (NSMutableArray *)getGategoryListWithDepartmentId:(int)departmentId
{
	NSString *key = [NSString stringWithFormat:@"%d", departmentId];
	NSMutableArray *array = [self.faultCategoryDic objectForKey:key];
	if(array == nil)
	{
		array = [NSMutableArray array];
		[self.faultCategoryDic setObject:array forKey:key];
	}
	return array;
}
@end
